export class ViewRightMouse {

    public ui: fgui.GObject;

    public isPressed: boolean;

    private _lastPos: Laya.Vector2;
    private _currentPos: Laya.Vector2;

    public dragVec: Laya.Vector2;
    public get isDrag(): boolean {
        return this.dragVec.x != 0 || this.dragVec.y != 0;
    }

    constructor(ui: fgui.GObject) {
        this.ui = ui;

        this.isPressed = false;
        this._lastPos = new Laya.Vector2(0, 0);
        this._currentPos = new Laya.Vector2(0, 0);
        this.dragVec = new Laya.Vector2(0, 0);

        this.ui.on(Laya.Event.RIGHT_MOUSE_DOWN, this, this._OnRightMouseDown);
        Laya.stage.on(Laya.Event.MOUSE_MOVE, this, this._OnMouseMove);
        Laya.stage.on(Laya.Event.RIGHT_MOUSE_UP, this, this._OnRightMosueUp);
    }

    public Dispose() {
        this.ui.off(Laya.Event.RIGHT_MOUSE_DOWN, this, this._OnRightMouseDown);
        Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this._OnMouseMove);
        Laya.stage.off(Laya.Event.RIGHT_MOUSE_UP, this, this._OnRightMosueUp);
    }

    private _OnRightMouseDown(event: Laya.Event) {
        if (this.isPressed) return;
        this.isPressed = true;
        this._lastPos.setValue(event.stageX, event.stageY);
        this._currentPos.setValue(event.stageX, event.stageY);
        this.dragVec.setValue(0, 0);
    }

    private _OnMouseMove(event: Laya.Event) {
        if (!this.isPressed) return;
        this._currentPos.setValue(event.stageX, event.stageY);
    }

    private _OnRightMosueUp(event: Laya.Event) {
        this.isPressed = false;
    }

    public LogicUpdate() {
        if (this.isPressed) {
            this.dragVec.setValue(this._currentPos.x - this._lastPos.x,
                this._currentPos.y - this._lastPos.y);
            this._lastPos.setValue(this._currentPos.x, this._currentPos.y);
        } else {
            this.dragVec.setValue(0, 0);
        }
    }

}